#!/bin/sh
# Script for sysstat simulation test environment
# (C) 2020-2025 Sebastien GODARD (sysstat <at> orange.fr)

# Get directory where current script is located
srcdir="${0%/*}"
export T_SRCDIR=$(realpath ${srcdir})

if [ ! -f ./sysstat.ioconf ]
then
	/bin/cp ${srcdir}/sysstat.ioconf .
fi

if [ "$1" = "conf" ]
then
	# Configure sysstat in test mode
	make distclean
	${srcdir}/configure sa_lib_dir=. sar_dir=. conf_dir=. conf_file=sysstat.sysconfig sa_dir=tests --enable-debuginfo --disable-stripping
elif [ "$1" = "conflto" ]
then
	# Configure sysstat in test mode with LTO support
	make distclean
	${srcdir}/configure sa_lib_dir=. sar_dir=. conf_dir=. conf_file=sysstat.sysconfig sa_dir=tests --enable-debuginfo --enable-lto --disable-stripping
elif [ "$1" = "comp" ]
then
	# Compile sysstat in test mode
	make simtest TFLAGS="-DTEST -DPRE_TESTDIR=\\\"${T_SRCDIR}\\\""
elif [ "$1" != "out" ]
then
	if [ "$1" != "sim" ]
	then
		# Configure then compile sysstat in test mode
		make distclean
#		./configure sa_lib_dir=. sar_dir=. conf_dir=. conf_file=sysstat.sysconfig sa_dir=tests --enable-debuginfo && make TFLAGS="-DTEST -DPRE_TESTDIR=\\\"${T_SRCDIR}\\\"" && make simtest
		${srcdir}/configure sa_lib_dir=. sar_dir=. conf_dir=. conf_file=sysstat.sysconfig sa_dir=tests --enable-debuginfo --disable-stripping && make simtest TFLAGS="-DTEST -DPRE_TESTDIR=\\\"${T_SRCDIR}\\\""
	fi
	rm -f tests/results.tmp
	COUNT=0
	TOTAL=`ls -1 ${srcdir}/tests/0* | wc -l`

	LIST=`ls ${srcdir}/tests | grep -E '^[0-9]+$' | sort -n`

	for f in ${LIST}; do

		if [ "$2" = cont -a -f tests/LAST ]
		then
			L=`cat tests/LAST`
			if [ $f -lt $L ]
			then
				continue
			else
				rm -f tests/LAST
			fi
		fi

		let "COUNT+=1"
		echo $f
		echo -n "$f: " >> tests/results.tmp
		cat ${srcdir}/tests/$f | /bin/sh

		if [ $? -eq 0 ]
		then
			if [ -f tests/SKIPPED ]
			then
				echo Skipped. >> tests/results.tmp
				rm tests/SKIPPED;
			else
				echo Success! >> tests/results.tmp;
			fi
		else
			echo Failed... >> tests/results.tmp;
			echo $f > tests/LAST;
			if [ "$1" != "ignore" -a "$2" != "ignore" ]
			then
				exit
			fi
		fi
	done
	rm -f tests/root
	ln -s ${T_SRCDIR}/tests/root1 tests/root
	echo Run ${COUNT}/${TOTAL} test\(s\)
	echo Simulation tests: Success!
fi

if [ "$?" = "0" -a "$1" = "all" ]
then
	make extratest && make distclean && ${srcdir}/configure --disable-nls --disable-sensors --disable-pcp sa_lib_dir=. sar_dir=. conf_dir=. conf_file=sysstat.sysconfig sa_dir=tests --enable-debuginfo --disable-stripping && make TFLAGS="-DTEST -DPRE_TESTDIR=\\\"${T_SRCDIR}\\\"" && echo "EXTRA TESTS: Success!"
fi

if [ "$1" = "out" ]
then
	# Test out-of-tree build
	make distclean
	mkdir -p /tmp/sysstat-out
	cd /tmp/sysstat-out && ${T_SRCDIR}/do_test
fi
